package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.STORE_SYS;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.cms.entity.Role;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.RoleMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class RoleFroAct {
     private static final Logger log=LoggerFactory.getLogger(RoleFroAct.class);
      
  	/**
  	 * 店铺角色添加页
  	 */
  	private static final String TPL_ROLE_SOTER_ADD="tpl.roleStoreAdd";
 	/**
 	 * 店铺角色修改页
 	 */
 	private static final String TPL_ROLE_STORE_EDIT="tpl.roleStoreEdit";
 	/**
 	 * 店铺角色列表页
 	 */
 	private static final String TPL_ROLE_STORE_LIST="tpl.roleStoreList";
 	
    @RequestMapping(value="/roleFro/list*.jspx")
    public String list(String cl,Integer pageNo,HttpServletRequest request,
   		 ModelMap model){
   	 Website web=SiteUtils.getWeb(request);
   	 ShopMember member=CmsUtils.getMember(request);
   	 //用户没有登录，跳转到登录页
   	 if(member==null){
   		 return ShopFrontHelper.showLogin(request, model, web);
   	 }
   	Store store=member.getStore();
	model.addAttribute("store", store);
	model.addAttribute("cl", cl);
   	 ShopFrontHelper.setCommonData(request, model, web);
   	 ShopFrontHelper.frontPageData(request, model);
   	 return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ROLE_STORE_LIST), request);
    }
    
    @RequestMapping(value="/roleFro/add.jspx")
    public String roleFroAdd(HttpServletRequest request,
   		 HttpServletResponse response,ModelMap model){
   	 Website web=SiteUtils.getWeb(request);
   	 ShopMember member=CmsUtils.getMember(request);
   	 //用户没有登录，跳转到登录页
   	 if(member==null){
   		 return ShopFrontHelper.showLogin(request, model, web);
   	 }
   	 Store store=member.getStore();
   	 model.addAttribute("store", store);
   	 ShopFrontHelper.setCommonData(request, model, web);
   	 ShopFrontHelper.frontPageData(request, model);
   	 return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ROLE_SOTER_ADD), request);
    }
    
    @RequestMapping(value="/roleFro/save.jspx")
    public String roleFroSave(Role bean,Long storeId, String[] perms,
    		HttpServletRequest request,HttpServletResponse response,
    		ModelMap model){
   	 Website web=SiteUtils.getWeb(request);
   	 ShopMember member=CmsUtils.getMember(request);
   	 //用户没有登录，跳转到登录页
   	 if(member==null){
   		 return ShopFrontHelper.showLogin(request, model, web);
   	 }
   	 manager.save(bean,web.getId(),storeId,splitPerms(perms));
   	 ShopFrontHelper.setCommonData(request, model, web);
   	 ShopFrontHelper.frontPageData(request, model);
   	 return "redirect:list.jspx";
    }
    
    //店铺角色修改页list传值为空
    @RequestMapping(value="/roleFro/edit.jspx")
    public String roleFroEdit(Long id,HttpServletRequest request,
   		 HttpServletResponse response,ModelMap model){
   	 Website web=SiteUtils.getWeb(request);
   	 ShopMember member=CmsUtils.getMember(request);
   	 //用户没有登录，跳转到登录页
   	 if(member==null){
   		 return ShopFrontHelper.showLogin(request, model, web);
   	 }
   	 Role bean =manager.findById(id);
   	 //传递页面
   	 model.addAttribute("role", bean);
   	 ShopFrontHelper.setCommonData(request, model, web);
   	 ShopFrontHelper.frontPageData(request, model);
   	 return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_ROLE_STORE_EDIT), request);
    }
    
    @RequestMapping(value="/roleFro/update.jspx")
    public String roleFroUpdate(Role bean,String[] perms,HttpServletRequest request,
   		 HttpServletResponse response,ModelMap model){
   	 Website web=SiteUtils.getWeb(request);
   	 ShopMember member=CmsUtils.getMember(request);
   	 //用户没有登录，跳转到登陆页
   	 if(member==null){
   		 return ShopFrontHelper.showLogin(request, model, web);
   	 }
   	 manager.update(bean,splitPerms(perms));
   	 return "redirect:list.jspx";
    }
    
	@RequestMapping("/roleFro/delete.jspx")
	public String delete(Long[] ids, HttpServletRequest request,
			ModelMap model) {
		Role[] beans = manager.deleteByIds(ids);
		for (Role bean : beans) {
			log.info("delete CmsRole id={}", bean.getId());
		}
		return list(null,null,request, model);
	}
     
 	private Set<String> splitPerms(String[] perms) {
		Set<String> set = new HashSet<String>();
		if (perms != null) {
			for (String perm : perms) {
				for (String p : StringUtils.split(perm, ',')) {
					if (!StringUtils.isBlank(p)) {
						set.add(p);
					}
				}
			}
		}
		return set;
	}
     
     @Autowired
     private RoleMng manager;
	
	
}
